import {ajaxFun} from "../../../utils/ajax"
Page({

  data: {
    nameK:"硕硕真棒",
    orderno:"",//订单号码
    orderNo:"",
    name: 1,
    address: {}, //地址信息  这里存放一个id
    orderInfoVo: {}, //商品信息

    latitude: "",
    longitude: "",
    zitiAddress:[],
    show:false,
    couponsList:[],//优惠券列表
    chooseCopons:"请选择",//默认文本
    couponId:0,//优惠券id
    useIntegral:false,//是否使用积分
    shippingType:1,// 快递支付1   到店支付 2
    payChannel:"weixinh5",//固定******************
    payType:"weixin",//默认微信支付
    payList:[//支付方式
       {
         id:1,
         name:"微信",
         imgUrl:"微信图标",
         payType:"weixin"
       },
       {
         id:2,
         name:"余额",
         imgUrl:"余额图标",
         payType:"yue"
       },
      //  {
      //    id:3,
      //    name:"支付宝",
      //    imgUrl:"支付宝图标",
      //    payType:"alipay"
      //  }
    ],
    i:0,
    mark:"",//备注
    realName:"",//联系人，
    phone:"",//手机号
    alipayRequest:""
  },


  onLoad(options) {

    this.getGoodsdes(options.preOrderNo);
    this.setData({
       orderno:options.preOrderNo
    },()=>{
        this.getCoupons()
    })
    wx.getLocation({//获取经纬度
      type: 'wgs84',
      success: (res) => {
        let {latitude,longitude} = res;
        this.setData({
          latitude,
          longitude
        },()=>{
          this.getZitiAddress()
        })

      }
    })
  },
  async getGoodsdes(orderno) { //根据订单号获取商品信息和地址信息
    let {
      orderInfoVo
    } = await ajaxFun(`/order/load/pre/${orderno}`, "get");
    this.getAddress(orderInfoVo.addressId)
    this.setData({
      orderInfoVo
    })
  },
  async getAddress(addressid) { // 根据地址id获取  地址信息
    let address = await ajaxFun(`/address/detail/${addressid}`, "get")
    this.setData({
      address
    })
  },

  onClick(e) {
    let name = e.detail.name
    this.setData({
      shippingType:name
    })
  },
  async getZitiAddress() {//到店自提
    let {list} = await ajaxFun("/store/list", "post", {
      latitude:this.data.latitude,
      longitude:this.data.longitude,
      page: 1,
      limit: 10
    })
    let newArry=list.filter(item=>item.name=="中心大楼")
    this.setData({
      zitiAddress:newArry
    })
  },
  onClose(){
    this.setData({
      show:false
    })
 },
 async getCoupons(){//优惠券
     let res=await ajaxFun(`/coupons/order/${this.data.orderno}`,"get")
     this.setData({
      couponsList:res,
     })
  },
  showCoupons(){//优惠券
     this.setData({
      show:true
     })
  },




  useCopons(e){// 可以使用
     let index=e.target.dataset.index;
     let couponId=this.data.couponsList[index].id//优惠券id

     let couponsList=this.data.couponsList;
     couponsList.map((item,i)=>{
         index==i?item.status=1:item.status=0
     })
 
  
     this.setData({
       couponId,
       couponsList,
       chooseCopons:couponsList[index].name,
       show:false
     },()=>{
         this.computedPrice()
     })

  },
  noCopons(){// 取消使用
    let couponsList=this.data.couponsList;
     couponsList.map((item,i)=>{
         item.status=0
     })
     this.setData({
       couponsList,
       show:false,
       chooseCopons:"请选择",//默认文本
       couponId:0
     },()=>{
       this.computedPrice()
     })
  },
  onChange(event) {//是否使用积分切换
    this.setData({
      useIntegral: event.detail,
    },()=>{
      this.computedPrice()
    });
  },
  async computedPrice(){//重新计算总价
    let {surplusIntegral,proTotalFee,payFee}=await ajaxFun("/order/computed/price","post",{
      addressId:this.data.address.id,//快递地址id
      couponId:this.data.couponId,//优惠券id
      preOrderNo:this.data.orderno,//订单号
      shippingType: this.data.shippingType,/// 快递支付1   到店支付 2
      useIntegral: this.data.useIntegral//是否使用积分
    })
    let orderInfoVo=this.data.orderInfoVo;//拿到商品的所有信息
    orderInfoVo.userIntegral=surplusIntegral;//用户积分
    orderInfoVo.proTotalFee=proTotalFee;//总价
    orderInfoVo.payFee=payFee;//合计价格
    this.setData({
      orderInfoVo:orderInfoVo
    })
  },
  changePay(e){//支付方式切换
    let i=e.target.dataset.index
       this.setData({
         i,
         payType:this.data.payList[i].payType
       })
  },
  onClickButton(){// 创建订单号
    
        if(this.data.orderNo){
          this.payFor(this.data.orderNo)
        }else{
          this.createOrder()
        }
     

  },
 async createOrder(){
    let  obj={
      addressId: this.data.address.id,
      couponId: this.data.couponId,
      mark: this.data.mark,
      payChannel: this.data.payChannel,
      payType: this.data.payType,
      phone:this.data.phone,
      preOrderNo: this.data.orderno,
      realName:this.data.realName,
      shippingType: this.data.shippingType,
      storeId: this.data.shippingType==1?0:this.data.zitiAddress[0].id,
      useIntegral: this.data.useIntegral
     }
     let {orderNo}=await ajaxFun("/order/create","post",obj);
   
         this.payFor(orderNo)
    
  },
async  payFor(orderNo){//支付成功 跳转页面

   let res=await ajaxFun("/pay/payment","post",{
    orderNo:this.data.orderNo?this.data.orderNo:orderNo,
    payChannel: this.data.payChannel,
    payType: this.data.payType,
    scene: 0
   })
   if(res){
      console.log("支付成功跳转页面");
       wx.redirectTo({
        url: `/indexPackage/pages/payOrder/payOrder?orderNo=${orderNo}`,
      })
   }else{
     console.log("支付失败");
      this.setData({
        orderNo
      })
   }
   
     
   

  }


 
 




})